#!/bin/bash
# Copyright 2012 Google Inc.  All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License.  You may obtain a copy
# of the License at: http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distrib-
# uted under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied.  See the License for
# specific language governing permissions and limitations under the License.

# Run a test continously.
#
# Usage:
#
# tools/ctest about  # runs js/about_test.js continuously
# tools/ctest about_test  # same as above, runs js/about_test.js

pushd "$(dirname $0)" >/dev/null && source common.sh && popd >/dev/null

# Decide which test to run.
if [ "$1" == "all" ]; then
    test_names=test
elif [ -n "$1" ]; then
    while [ -n "$1" ]; do
        test_names="$test_names ${1%_test}_test"
        shift
    done
else
    echo "Usage: $0 foo_test bar_test ..."
    exit 1
fi

# A temporary file for the output of gjstest.
output_file=/tmp/gjstest.out.$$

# Run the test repeatedly as frequently as possible.
cd $APP_DIR
while true; do
    make $test_names > $output_file 2>&1
    # Get the number of rows in the terminal, minus a few in case lines wrap.
    # stty says "...; rows 100; ..." on Linux but "...; 100 rows; ..." on BSD.
    rows=$(stty -a | head -1 | sed -e 's/.*;\(.*rows[^;]*\).*/\1/' | tr -dC 0-9)
    rows=$(expr $rows - 10)
    clear
    head -$rows $output_file
done
